package sjd.ahsj.springbootmybatisplusoracle.crontab;

import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.google.common.collect.Lists;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import sjd.ahsj.springbootmybatisplusoracle.entity.Anime;
import sjd.ahsj.springbootmybatisplusoracle.entity.Section;
import sjd.ahsj.springbootmybatisplusoracle.service.IAnimeService;
import sjd.ahsj.springbootmybatisplusoracle.service.IAnimeTypeService;
import sjd.ahsj.springbootmybatisplusoracle.service.ISectionService;
import sjd.ahsj.springbootmybatisplusoracle.util.HttpRequest;
import java.util.Date;
import java.util.List;

/**
 * 动漫书籍抓取
 *
 * @author zhanghaogao
 * @create 2018-09-18 下午12:52
 **/
@Component
public class AnimeCrawl {
    @Autowired
    private IAnimeService iAnimeService;
    @Autowired
    private ISectionService iSectionService;
    @Autowired
    private IAnimeTypeService iAnimeTypeService;


//    @Scheduled(cron = "0 0 0/12 * * ?")
    public void animeCrawl() {

        for (int e = 1; e <= 38; e++) {
            String html = HttpRequest.sendGet("http://m.gumua.com/Manhua/" + e + "/", "");
            Document doc = Jsoup.parse(html);
            List<String> urls = doc.select("body > ul > li > a").eachAttr("href");
            for (String url : urls) {
                html = HttpRequest.sendGet("http://m.gumua.com" + url, "");
                doc = Jsoup.parse(html);
                //判断是否增加漫画
                Anime anime;
                if (iAnimeService.selectCount(new EntityWrapper<Anime>().eq("original_id",
                        url.substring(url.lastIndexOf("/"), url.length()).trim())) == 0) {
                    anime = new Anime();
                    anime.setOriginalId(url.substring(url.lastIndexOf("/"), url.length()).trim());
                    anime.setName(doc.select("body > div.d_header > div.ov.d_he > div.d_he_r > a").text());
                    anime.setUpTime(new Date());
                    List<String> type = Lists.newArrayList();
                    type.add(iAnimeTypeService.selectById(e).getName());
                    anime.setType(JSONArray.toJSONString(type));
                    anime.setCoverUrl(doc.select("body > div.d_header > div.ov.d_he > a.fl.d_he_img > img").attr("src"
                    ));
                    anime.setAbout(doc.select("body > div.d_header > div.d_he_tit").text());
                    if (doc.select("body > div.d_header > div.ov.d_he > div.d_he_r > p.d_he_rp1").text().equals(
                            "状态：连载中")) {
                        anime.setFinish(0);
                    } else {
                        anime.setFinish(1);
                    }
                    anime.setCarousel(0);
                    anime.setSubscriptionNum(0);
                    anime.setReadNum(0);
                    iAnimeService.insert(anime);
                } else {
                    anime = iAnimeService.selectOne(new EntityWrapper<Anime>().eq("original_id",
                            url.substring(url.lastIndexOf("/"), url.length()).trim()));
                    anime.setName(doc.select("body > div.d_header > div.ov.d_he > div.d_he_r > a").text());
                    anime.setCoverUrl(doc.select("body > div.d_header > div.ov.d_he > a.fl.d_he_img > img").attr("src"
                    ));
                    anime.setAbout(doc.select("body > div.d_header > div.d_he_tit").text());
                    List<String> type = JSONArray.parseArray(anime.getType(), String.class);
                    String name = iAnimeTypeService.selectById(e).getName();
                    type.remove(name);
                    type.add(name);
                    anime.setType(JSONArray.toJSONString(type));
                    if (doc.select("body > div.d_header > div.ov.d_he > div.d_he_r > p.d_he_rp1").text().equals(
                            "状态：连载中")) {
                        anime.setFinish(0);
                    } else {
                        anime.setFinish(1);
                    }
                }
                //获取章节
                List<String> se = doc.select("body > div.d_cen > ul > li > a").eachAttr("href");
                for (int i = se.size() - 1; i >= 0; i--) {
                    if (iSectionService.selectCount(new EntityWrapper<Section>().eq("original_id",
                            se.get(i).substring(se.get(i).lastIndexOf("/"), se.get(i).length()).trim())) == 0) {
                        Section section = new Section();
                        section.setOriginalId(se.get(i).substring(se.get(i).lastIndexOf("/"), se.get(i).length()).trim());
                        String info = HttpRequest.sendGet("http://m.gumua.com" + se.get(i), "");
                        doc = Jsoup.parse(info);
                        section.setName(doc.select("body > div.read_tit > h1").text());
                        section.setContent(JSONArray.toJSONString(doc.select("body > div.l_cen > img").eachAttr("src")));
                        section.setAnimeId(anime.getId());
                        int sum = iSectionService.selectCount(new EntityWrapper<Section>().eq("anime_id",
                                anime.getId()));
                        section.setAmount(sum > 5 ? 50L : 0L);
                        anime.setUpTime(new Date());
                        iSectionService.insert(section);
                    }
                }
                iAnimeService.updateById(anime);
            }

        }
        //设置首页轮播
        String carousel = HttpRequest.sendGet("http://m.gumua.com/", "");
        Document caDoc = Jsoup.parse(carousel);

        Elements caRows = caDoc.select("body > div.m_banner > div > div > a > img");
        List<String> imgs = caRows.eachAttr("src");
        caRows = caDoc.select("body > div.m_banner > div > div > a");
        Anime up = new Anime();
        up.setCarousel(0);
        iAnimeService.update(up, new EntityWrapper<>());
        for (int i = 0; i < caRows.eachAttr("href").size(); i++) {
            String id = caRows.eachAttr("href").get(i);
            id = id.substring(id.lastIndexOf("/"), id.length());
            up = iAnimeService.selectOne(new EntityWrapper<Anime>().eq("original_id", id));
            up.setCarouselImg(imgs.get(i));
            up.setCarousel(1);
            iAnimeService.updateById(up);
        }

    }

//    @Scheduled(cron = "0 0 0/8 * * ?")
//    public void animeCrawl() {
//
//        for (int e = 1; e < e + 1; e++) {
//            //获取所有的动漫书籍
//            String json = HttpRequest.sendGet("http://baimujia.com/api/classify/0-0-0-0-0-" + e, "");
//            ListModel listModel = JSONObject.parseObject(json, ListModel.class);
//            if (listModel.getData().getComics().size() == 0) {
//                break;
//            }
//            for (ListInfo info : listModel.getData().getComics()) {
//                Anime anime;
//                if (iAnimeService.selectCount(new EntityWrapper<Anime>().eq("original_id", info.getId().trim())) ==
// 0) {
//                    //新动漫
//                    anime = new Anime(info.getId(), info.getTitle(), new Date(), null, info.getCover(),
//                            info.getAuthorstr(), null, 0, info.getIsend(), null, 0, 0, 0);
//                    iAnimeService.insert(anime);
//                } else {
//                    //修改最近更新时间
//                    anime = iAnimeService.selectOne(new EntityWrapper<Anime>().eq("original_id", info.getId().trim
// ()));
//                    //anime.setUpTime(new Date());
//                    anime.setFinish(info.getIsend());
//                    anime.setName(info.getTitle().trim());
//                    anime.setCoverUrl(info.getCover());
//                    anime.setAuthor(info.getAuthorstr());
////                    iAnimeService.updateById(anime);
//                }
//                //获取漫画章节列表
//                String chapter = HttpRequest.sendGet("http://baimujia.com/1PRQNO/comic/" + info.getId(), "");
//                Document doc = Jsoup.parse(chapter);
//                Elements rows = doc.select("a[class=pd]");
//                for (String td : rows.eachText()) {
//                    //漫画分类
//                    if (iAnimeTypeService.selectCount(new EntityWrapper<AnimeType>().eq("name", td.trim())) == 0) {
//                        iAnimeTypeService.insert(new AnimeType(0, td.trim()));
//                    }
//                }
//                anime.setType(JSONArray.toJSONString(rows.eachText()));
//                anime.setAbout(doc.select("body > div.Introduct > p.txtDesc.autoHeight").text());
//                //获取章节内容
//                chapter = doc.select("body > script:nth-child(6)").get(0).toString().trim();
//                chapter = chapter.substring(chapter.indexOf("initIntroData([") + 15, chapter.lastIndexOf("]}]);") +
// 2);
//                SectionModel sectionModel = JSONObject.parseObject(chapter, SectionModel.class);
//                for (SectionList sectionList : sectionModel.getData()) {
//                    if (iSectionService.selectCount(new EntityWrapper<Section>().eq("original_id",
//                            sectionList.getId().trim())) == 0) {
//                        Section section = new Section(sectionList.getId(), sectionList.getChapter_name().trim(),
//                                sectionList.getChapter_cover(), sectionList.getGold(), null,
//                                sectionList.getChapter_order(), 0, anime.getId());
//                        //获取内容详情
//                        String content =
//                                HttpRequest.sendGet("http://baimujia.com/1PRQNO/view/" + info.getId() + "/" +
// section.getOriginalId(), "");
//                        Document doc2 = Jsoup.parse(content);
//                        Elements rows2 = doc2.select("img[class=comic_img lazy]");
//                        section.setContent(JSONArray.toJSONString(rows2.eachAttr("data-original")));
//                        iSectionService.insert(section);
//                        anime.setUpTime(new Date());
//
//
//                    }
//                }
//                iAnimeService.updateById(anime);
//
//
//            }
//        }
//        //设置首页轮播
//        String carousel = HttpRequest.sendGet("http://baimujia.com/1PRQ95", "");
//        Document caDoc = Jsoup.parse(carousel);
//        Elements caRows = caDoc.select("body > div.main_visual > div.main_image > ul > li > a >img");
//        List<String> imgs = caRows.eachAttr("src");
//        caRows = caDoc.select("body > div.main_visual > div.main_image > ul > li > a");
//        Anime up = new Anime();
//        up.setCarousel(0);
//        iAnimeService.update(up, new EntityWrapper<>());
//        for (int i = 0; i < caRows.eachAttr("href").size(); i++) {
//            String id = caRows.eachAttr("href").get(i);
//            id = id.substring(id.lastIndexOf("/") + 1, id.length());
//            up = iAnimeService.selectOne(new EntityWrapper<Anime>().eq("original_id", id));
//            up.setCarouselImg(imgs.get(i));
//            up.setCarousel(1);
//            iAnimeService.updateById(up);
//        }
//
//
//    }
}
